﻿var $ = layui.jquery;

var id = location.search.split('=')[1]

layui.use(['table', 'jquery', 'layer'], function () {
    function editRowBtn() {
        $.ajax({
            type: 'GET',
            url: `http://10.31.60.18:8000/ProduceGetRout/api/GermlineHistoryRecordsRead/GetGermlineHistoryRecordsInfo?id=${id}`, // 根据实际情况修改ControllerName和ActionName
            success: function (response) {
                var data = response.data
                console.log(response.data)
                $("#id").val(data.id)
                $("#motherEarHorn").val(data.motherEarHorn)
                $("#dateOfBreeding").val(data.dateOfBreeding != null ? data.dateOfBreeding.split('T')[0] : data.dateOfBreeding)
                $("#breedEarHorn").val(data.breedEarHorn)
                $("#pregnancyTestDate").val(data.pregnancyTestDate != null ? data.pregnancyTestDate.split('T')[0] : data.pregnancyTestDate)
                $("#pregnancyTestResult").val(data.pregnancyTestResult)
                $("#deliveryDate").val(data.deliveryDate != null ? data.deliveryDate.split('T')[0] : data.deliveryDate)
                $("#healthyNumber").val(data.healthyNumber)
                $("#healthyTotalWeight").val(data.healthyTotalWeight)
                $("#weakLambNumber").val(data.weakLambNumber)
                $("#deformityNumber").val(data.deformityNumber)
                $("#numberOfStillbirths").val(data.numberOfStillbirths)
                $("#mummyNumber").val(data.mummyNumber)
                $("#numberOfFreaks").val(data.numberOfFreaks)
                $("#currentParity").val(data.currentParity)
                $("#weaningDate").val(data.weaningDate != null ? data.weaningDate.split('T')[0] : data.weaningDate)
                $("#weaningNumber").val(data.weaningNumber)
                $("#weaningLitterWeight").val(data.weaningLitterWeight)
            },
            error: function (xhr, status, error) {
                // 处理错误响应
                console.error(error);
            }
        })
    }

    //绑定耳号
    $(document).ready(function () {
        $.ajax({
            url: 'http://10.31.60.18:8000/ArchivesGetRout/api/Archives/ArchivesAllInfo',
            type: 'get',
            dataType: 'json',
            success: function (res) {
                var enumData = res; // 直接使用返回的数据
                var select = $('#motherEarHorn');
                $.each(enumData, function (index, item) {
                    if (item.sheepType == "种母") {
                        // 使用正确的字段添加选项到下拉框中
                        var option = $('<option>').val(item.earNnumber).text(item.earNnumber);
                        select.append(option);
                    }
                });
                // 手动触发 layui 组件的渲染
                layui.form.render('select');
            },
            error: function () {
                // 处理 AJAX 请求失败的情况
                console.error('Failed to fetch data for dropdown.');
            }
        });
    });

    //绑定种公耳号
    $(document).ready(function () {
        $.ajax({
            url: 'http://10.31.60.18:8000/ArchivesGetRout/api/Archives/ArchivesAllInfo',
            type: 'get',
            dataType: 'json',
            success: function (res) {
                var enumData = res; // 直接使用返回的数据
                var select = $('#breedEarHorn');
                $.each(enumData, function (index, item) {
                    if (item.sheepType == "种公") {
                        // 使用正确的字段添加选项到下拉框中
                        var option = $('<option>').val(item.earNnumber).text(item.earNnumber);
                        select.append(option);
                    }
                });
                // 手动触发 layui 组件的渲染
                layui.form.render('select');
            },
            error: function () {
                // 处理 AJAX 请求失败的情况
                console.error('Failed to fetch data for dropdown.');
            }
        });
    });

    //绑定妊检结果
    $(document).ready(function () {
        $.ajax({
            url: 'http://10.31.60.18:8000/ProduceGetRout/api/PregnancyPregnancyTestRead/GetPregnancyTestResult',
            type: 'get',
            dataType: 'json',
            success: function (res) {
                var enumData = res; // 直接使用返回的数据
                var select = $('#pregnancyTestResult');
                $.each(enumData, function (index, item) {
                    // 使用正确的字段添加选项到下拉框中
                    var option = $('<option>').val(item.name).text(item.name);
                    select.append(option);
                });
                // 手动触发 layui 组件的渲染
                layui.form.render('select');
            },
            error: function () {
                // 处理 AJAX 请求失败的情况
                console.error('Failed to fetch data for dropdown.');
            }
        });
    });
    $(function () {
        setTimeout(function () {
            editRowBtn()
        }, 100)
    })

    //修改
    $('#updRowBtn').on('click', function () {
        // 获取单个字段的输入框元素
        const motherEarHorn = document.getElementById('motherEarHorn');

        if (motherEarHorn.value.trim() === '') {
            layer.msg('种母耳号不能为空！');
            return;
        }

        // 获取单个字段的输入框元素
        const dateOfBreeding = document.getElementById('dateOfBreeding');

        if (dateOfBreeding.value.trim() === '') {
            layer.msg('配种日期不能为空！');
            return;
        }

        // 获取单个字段的输入框元素
        const currentParity = document.getElementById('currentParity');

        if (currentParity.value.trim() === '') {
            layer.msg('当前胎次不能为空！');
            return;
        }

        var form = {
            id: $("#id").val(),
            motherEarHorn: $("#motherEarHorn").val() == "" ? null : $("#motherEarHorn").val(),
            dateOfBreeding: $("#dateOfBreeding").val() == "" ? null : $("#dateOfBreeding").val(),
            breedEarHorn: $("#breedEarHorn").val() == "" ? null : $("#breedEarHorn").val(),
            pregnancyTestDate: $("#pregnancyTestDate").val() == "" ? null : $("#pregnancyTestDate").val(),
            pregnancyTestResult: $("#pregnancyTestResult").val() == "" ? null : $("#pregnancyTestResult").val(),
            deliveryDate: $("#deliveryDate").val() == "" ? null : $("#deliveryDate").val(),
            healthyNumber: $("#healthyNumber").val() == "" ? null : $("#healthyNumber").val(),
            healthyTotalWeight: $("#healthyTotalWeight").val() == "" ? null : $("#healthyTotalWeight").val(),
            weakLambNumber: $("#weakLambNumber").val() == "" ? null : $("#weakLambNumber").val(),
            deformityNumber: $("#deformityNumber").val() == "" ? null : $("#deformityNumber").val(),
            numberOfStillbirths: $("#numberOfStillbirths").val() == "" ? null : $("#numberOfStillbirths").val(),
            mummyNumber: $("#mummyNumber").val() == "" ? null : $("#mummyNumber").val(),
            numberOfFreaks: $("#numberOfFreaks").val() == "" ? null : $("#numberOfFreaks").val(),
            currentParity: $("#currentParity").val() == "" ? null : $("#currentParity").val(),
            weaningDate: $("#weaningDate").val() == "" ? null : $("#weaningDate").val(),
            weaningNumber: $("#weaningNumber").val() == "" ? null : $("#weaningNumber").val(),
            weaningLitterWeight: $("#weaningLitterWeight").val() == "" ? null : $("#weaningLitterWeight").val()
        }

        console.log(form)

        $.ajax({
            type: 'PUT',
            url: 'http://10.31.60.18:8000/ProduceSetRout/api/GermlineHistoryRecords/UpdateGermlineHistoryRecords', // 根据实际情况修改ControllerName和ActionName
            data: JSON.stringify(form),
            contentType: "application/json",
            success: function (response) {
                if (response.code == 200) {
                    layer.msg('修改成功', { icon: 1 }, function () {
                        location.href = '/GermlineHistoryRecordsRead/GermlineShow'
                    });
                } else {
                    layer.alert('修改失败', {
                        icon: 1,
                        shadeClose: true,
                    })
                }
            },
            error: function (xhr, status, error) {
                // 处理错误响应
                console.error(error);
            }
        });
    })
});